package com.dwcode.macro.controller;

import com.dwcode.macro.dto.Pagination;
import com.dwcode.macro.dto.ResultInfo;
import com.dwcode.macro.entity.TProduct;
import com.dwcode.macro.entity.TUser;
import com.dwcode.macro.service.TProductService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * (TProduct)表控制层
 *
 * @author makejava
 * @since 2020-09-21 09:38:49
 */
@RestController
@RequestMapping("tProduct")
@Slf4j
public class TProductController {
    /**
     * 服务对象
     */
    @Resource
    private TProductService tProductService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TProduct selectOne(Integer id) {
        return this.tProductService.queryById(id);
    }

    @GetMapping("/getProducts")
    public Pagination<TProduct> getProducts(Pagination pagination) {
        return tProductService.getProducts(pagination);
    }

    @PutMapping("/setProduct")
    @ResponseBody
    @ApiOperation("修改产品信息")
    public ResultInfo<Pagination<TProduct>> updateProduct(TProduct product, Pagination pagination){
        ResultInfo<Pagination<TProduct>> resultInfo = new ResultInfo<>();
        log.info("product:" + product + "   pagination:" + pagination);
        TProduct tProduct = tProductService.update(product);
        if (tProduct != null){
            resultInfo.setCode(1);
            resultInfo.setMsg("修改产品信息成功");
        }
        pagination = tProductService.getProducts(pagination);
        resultInfo.setResult(pagination);
        return resultInfo;
    }
    @DeleteMapping("/deleteProduct/{id}")
    @ResponseBody
    @ApiOperation("删除产品信息")
    public ResultInfo<Pagination<TProduct>> deleteProduct(@PathVariable("id")Integer id, Pagination pagination){
        ResultInfo<Pagination<TProduct>> resultInfo = new ResultInfo<>();
        boolean b = tProductService.deleteById(id);
        if (b){
            resultInfo.setCode(1);
            resultInfo.setMsg("删除产品成功");
        }
        pagination = tProductService.getProducts(pagination);
        resultInfo.setResult(pagination);
        return resultInfo;
    }

    @PostMapping("/addProduct")
    @ApiOperation("添加产品信息")
    public ResultInfo<Pagination<TProduct>> insertProduct(TProduct product,Pagination pagination){
        ResultInfo<Pagination<TProduct>> resultInfo = new ResultInfo<>();
        log.info("product" + product);
        TProduct tProduct = tProductService.insert(product);
        if (tProduct != null){
            resultInfo.setCode(1);
            resultInfo.setMsg("添加产品信息成功");
        }
        pagination = tProductService.getProducts(pagination);
        if(pagination.getTotal() % pagination.getPageSize() == 1){
            pagination.setPageNum(pagination.getPageNum() + 1);
        }
        resultInfo.setResult(pagination);
        return resultInfo;
    }

    /**
     * 批量删除
     */
    @GetMapping("/deleteBatch")
        public @ResponseBody ResultInfo<Pagination<TProduct>> deleteBatch(String ids,Pagination pagination){
            log.info(ids);
            String[] iss = ids.split(",");
            int[] array = Arrays.asList(iss).stream().mapToInt(Integer::parseInt).toArray();
            boolean i = tProductService.deleteBatch(array);
            ResultInfo<Pagination<TProduct>> resultInfo = new ResultInfo<>();
            if (i){
                resultInfo.setCode(1);
                resultInfo.setMsg("批量删除成功");
            }else{
                resultInfo.setCode(0);
                resultInfo.setMsg("批量删除失败");
                return resultInfo;
            }
            pagination = tProductService.getProducts(pagination);
            resultInfo.setResult(pagination);
            return resultInfo;
        }
    /**
     * 模糊查询 产品信息
     */
    @GetMapping("/searchProducts")
    public @ResponseBody ResultInfo<Pagination<TProduct>> searchProducts(TProduct product,Pagination<TProduct> pagination){
        log.info("product:" + product);
        ResultInfo<Pagination<TProduct>> resultInfo = new ResultInfo<>();
        List<TProduct> tProducts = tProductService.searchProducts(product);
        if (tProducts.size() == 0){
            resultInfo.setCode(0);
            pagination.setRows(null);
            pagination.setTotal(0);
            resultInfo.setMsg("未查询到该信息");
            return resultInfo;
        }
        resultInfo.setCode(1);
        pagination.setRows(tProducts);
        pagination.setTotal(tProducts.size());
        resultInfo.setResult(pagination);
        return resultInfo;
    }

}